<?php
class ModelArticleCategory extends Model {
	
	public function getCategory($category_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id)  WHERE c.category_id = '" . (int)$category_id . "' AND c.status = '1'");
		
		return $query->row;
	}
	
    public function getCategories($parent_id = 0) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");
		
		return $query->rows;
	}
    public function getTopCategory()
    {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category WHERE top = '1' AND status='1' ORDER BY sort_order ");
        return $query->rows;
    }
    
    public function getPath($category_id) {
		$query = $this->db->query("SELECT c.category_id, parent_id FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) WHERE c.category_id = '" . (int)$category_id. "' ORDER BY c.sort_order, cd.name ASC");
		
		$category_info = $query->row;
		
		if ($category_info['parent_id']) {
			return $this->getPath($category_info['parent_id']) . '_' . $category_info['category_id'];
		} else {
			return $category_info['category_id'];
		}
	}
    public function getCategoriesByParentId($category_id) {
		$category_data = array();
		
		$category_data[] = $category_id;
		
		$category_query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "category WHERE parent_id = '" . (int)$category_id . "'");
		
		foreach ($category_query->rows as $category) {
			$children = $this->getCategoriesByParentId($category['category_id']);
			
			if ($children) {
				$category_data = array_merge($children, $category_data);
			}			
		}
		
		return $category_data;
	}
}
?>